package member.controller;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import member.model.service.MemberService;
import member.to.ActionForward;
import member.to.MemberTO;

public class RemoveMemberController implements Controller {
	
	@Override
	public ActionForward handle(HttpServletRequest request,
			HttpServletResponse response){
		HttpSession session = request.getSession();
		MemberTO loginInfo = (MemberTO)session.getAttribute("login_info");
		ActionForward af = null;
		if(loginInfo == null){//로그인 안된 경우
			request.setAttribute("error_message", "로그인 후 탈퇴가 가능합니다. 로그인 먼저 하세요");
			af = new ActionForward("/login_form.jsp", false);
		}else{
			try {
				MemberService mms = MemberService.getInstance();
				mms.removeMemberById(loginInfo.getId());
				af = new ActionForward("/login_form.jsp", false);
//				세션 소멸시키기
				session.invalidate();
			}catch (SQLException e) {
				e.printStackTrace();
				request.setAttribute("error_message", e.getMessage());
				af = new ActionForward("/error.jsp", false);
			} 
		}
		return af;
	}

}
